// @ts-ignore
(function (window: any, document: any) {
    const hearts: any = [];
    window.requestAnimationFrame = (function () {
        return window.requestAnimationFrame ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.oRequestAnimationFrame ||
            window.msRequestAnimationFrame ||
            function (callback: any) {
                setTimeout(callback, 1000 / 60);
            }
    })();
    init();

    function init() {
        css(".heart{width: 1px;height: 1px;position: fixed;");
        attachEvent();

        gameLoop();
    }

    function gameLoop() {
        for (let i = 0; i < hearts.length; i++) {
            if (hearts[i].alpha <= 0) {
                document.body.removeChild(hearts[i].el);
                hearts.splice(i, 1);
                continue;
            }
            hearts[i].y++;
            hearts[i].x += hearts[i].xx;
            hearts[i].scale -= 0.01;
            hearts[i].alpha -= 0.008;
            hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);color:" + hearts[i].color;
        }
        requestAnimationFrame(gameLoop);

    }

    function attachEvent() {
        const old = typeof window.onmousemove === "function" && window.onmousemove;
        window.onmousemove = function (event:any) {
            old && old();
            createHeart(event);
        }
    }

    function createHeart(event: any) {
        const d = document.createElement("samp");
        d.className = "heart";
        d.innerHTML = "*";
        hearts.push({
            el: d,
            x: event.clientX - 8,
            y: event.clientY - 13,
            xx: Math.pow(-1, (Math.round(Math.random()))) * Math.random(),
            scale: 1,
            alpha: 1,
            color: randomColor()
        });
        document.body.appendChild(d);
    }

    function css(css: any) {
        const style = document.createElement("style");
        style.type = "text/css";
        try {
            style.appendChild(document.createTextNode(css));
        } catch (ex) {
            style.styleSheet.cssText = css;
        }
        document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
        return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
    }
})(window, document);
